
/* JavaScript content from js/issue/projectIssues.js in folder common */
/**
 * 
 */

currentPage = {};
projectIssues = {};
var project_id = null;
var projectIssuesList = null;

$(document).on("pagebeforeshow", "#projectIssues", function(event) {
	WL.Logger.debug("ProjectIssues::on::pagebeforeshow");
	busyIndicator.hide();
	project_id = sessionStorage.selectedProjectId;
	if (project_id) {
		projectIssues.getProjectIssues();
	}
});

projectIssues.getProjectIssues = function() {
	busyIndicator = new WL.BusyIndicator("content", {
		text : "Loading..."
	});
	busyIndicator.show();
	WL.Logger.debug("ProjectIssues::invokeProcedure");
	var invocationData = {
		adapter : "Redmine",
		procedure : "getProjectIssues",
		parameters : [ project_id ]
	};
	// busyIndicator.show();
	WL.Client
			.invokeProcedure(
					invocationData,
					{
						onSuccess : function(response) {

							if (response.invocationResult.statusCode == 200) {
								WL.Logger
										.debug("ProjectIssues::invokeProcedure::Successful");
								projectIssuesList = response.invocationResult.issues;
								projectIssues
										.renderProjectIssuesList(projectIssuesList);
							} else
								alert("Invoke getProjects Procedure Failure.StatusCode!=200. Response :: "
										+ JSON.stringify(error));

						},
						onFailure : function(response) {
							if (response.invocationResult.authStatus == "required") {
								customAuthenticatorRealmChallengeHandler
										.handleChallenge(response);
							} else {
								WL.Logger
										.debug("ProjectIssues::invokeProcedure::Failure. Error = "
												+ JSON.stringify(error));
								alert("Invoke getProjects Procedure Failure. Response :: "
										+ JSON.stringify(error));
							}
						}
					});
};

projectIssues.renderProjectIssuesList = function(projectIssuesList) {
	$('#projectIssueTitle').html(projectIssuesList[0].project.name);
	var $issues_listview = $('#project_issues_list');
	var $template = $('#project_issues_list .template');
	var $issue_item_template = $('#project_issues_list .sample-issue-item')
			.clone().removeClass('sample-issue-item template').addClass(
					'issue-item').show();
	$issues_listview.html('');

	if (!projectIssuesList || projectIssuesList.length == 0) {
		$template.appendTo($issues_listview).filter('.no-issues').show();
	} else {
		$.each(projectIssuesList, function(i, issue) {

			var issue_id = issue.id;
//			var issue_subject = issue.subject;
			var $issue_item = $issue_item_template.clone().attr({
				"issue-id" : issue_id,
				"issue-index" : i
			});
			//.append($("<a>").append("#" + issue_id + " | " + issue_subject));
			$issue_item.find(".issue-id").html("#" + issue_id);
			$issue_item.find(".issue-subject").html(issue.subject);
			$issue_item.find(".issue-tracker").html(issue.tracker.name);
			$issue_item.find(".issue-status").html(issue.status.name);
			$issue_item.find(".issue-priority").html(issue.priority.name);
			$issue_item.find(".issue-author").html(issue.author.name);
			$issue_item.find(".issue-updated-on").html(issue.updated_on);
			
			$issues_listview.append($issue_item);

		});
		$issues_listview.listview('refresh').append($template.hide());
	}
	busyIndicator.hide();

};
$(document).on("pageshow", "#projectIssues", function() {
	$('#project_issues_list').on('tap', '.issue-item', function(e) {
		e.stopImmediatePropagation();
		e.preventDefault();
//		var idx = $(this).attr("issue-index");

//		sessionStorage.selectedIssueId = projectIssuesList[idx].id;
		sessionStorage.selectedIssueId = $(this).attr("issue-id");
			$.mobile.changePage("IssueShow.html", {changeHash : false,transition: "slide"});
	});
});

